package cn.kailangtu.dataspot.camera;

import android.content.Context;
import android.net.Uri;
import android.view.SurfaceView;

import org.videolan.libvlc.LibVLC;
import org.videolan.libvlc.Media;
import org.videolan.libvlc.MediaPlayer;
import org.videolan.libvlc.interfaces.IVLCVout;
import org.videolan.libvlc.util.VLCVideoLayout;

import java.util.ArrayList;

public class VLCManager {
    private static final boolean USE_TEXTURE_VIEW = false;
    private static final boolean ENABLE_SUBTITLES = true;
    private static final int CACHE = 10;

    private VLCVideoLayout mVideoLayout;

    private LibVLC mLibVLC;
    private MediaPlayer mMediaPlayer;

    public VLCManager(Context context, VLCVideoLayout videoLayout) {
        ArrayList<String> options= new ArrayList<>();

        options.add("--rtsp-tcp");//强制rtsp-tcp，加快加载视频速度
        options.add("--aout=opensles");
        options.add("--audio-time-stretch");
        options.add("-vvv");
        options.add("--h264-fps=20");
        options.add("--hevc-fps=20");
        mLibVLC = new LibVLC(context, options);
        mMediaPlayer = new MediaPlayer(mLibVLC);

        mVideoLayout = videoLayout;
    }

    public void play(String url) {
        mMediaPlayer.attachViews(mVideoLayout, null, ENABLE_SUBTITLES, USE_TEXTURE_VIEW);

        Media media = new Media(mLibVLC, Uri.parse(url));
        media.addOption(":network-caching="+CACHE);
        mMediaPlayer.setMedia(media);
        media.release();

        mMediaPlayer.play();
    }

    public void play(SurfaceView textureView, String strUrl)
    {
        IVLCVout vout = mMediaPlayer.getVLCVout();

        vout.setVideoView(textureView);

        vout.attachViews();

        // String str_url = "https://qywapp.oss-cn-beijing.aliyuncs.com/2022/7/18/oss06ac675d-c493-2121-cfba-e5d627210036.mp4";
        //String str_url="rtsp://admin:hhll@192.168.8.166:554/Streaming/Channels/102";
        //String str_url = "https://media.w3.org/2010/05/sintel/trailer.mp4";

        //美国
        // String str_url = "rtmp://media3.scctv.net/live/scctv_800";
        //韩国
        // String str_url = "rtmp://mobliestream.c3tv.com:554/live/goodtv.sdp";
        // String str_url = "https://v.kuaishou.com/wwZ3v9";
        Uri uri = Uri.parse(strUrl);//rtsp流地址或其他流地址//"https://media.w3.org/2010/05/sintel/trailer.mp4"

        final Media media = new Media(mLibVLC, uri);

        int cache = 10;

        media.addOption(":network-caching=" + cache);

        media.addOption(":file-caching=" + cache);

        media.addOption(":live-cacheing=" + cache);

        media.addOption(":sout-mux-caching=" + cache);

        media.addOption(":codec=mediacodec,iomx,all");

        mMediaPlayer.setMedia(media);//

        media.setHWDecoderEnabled(false, false);//设置后才可以录制和截屏,这行必须放在mMediaPlayer.setMedia(media)后面，因为setMedia会设置setHWDecoderEnabled为true

        mMediaPlayer.setEventListener(new MediaPlayer.EventListener() {
            @Override
            public void onEvent(MediaPlayer.Event event) {
                // System.out.println("MainActivity.onEvent 111");
                if (event.type == MediaPlayer.Event.Playing) {
                    System.out.println("MainActivity.onEvent 正在播放");
                } else if (event.type == MediaPlayer.Event.Paused) {
                    System.out.println("MainActivity.onEvent Paused");
                } else if (event.type == MediaPlayer.Event.Opening) {
                    System.out.println("MainActivity.onEvent Opening");
                } else if (event.type == MediaPlayer.Event.Stopped) {
                    System.out.println("MainActivity.onEvent Stopped");
                } else if (event.type == MediaPlayer.Event.Vout) {
                    System.out.println("MainActivity.onEvent getVoutCount=" + event.getVoutCount());
                    //mHandler.sendEmptyMessageDelayed(UPDATE_SCREEN, 1000);
                } else {
                    System.out.println("MainActivity.onEvent else ");
                }

            }
        });
        mMediaPlayer.play();
    }

    public void onStop() {
        if (mMediaPlayer != null) {
            mMediaPlayer.stop();
            mMediaPlayer.detachViews();
        }
    }

    public void onDestroy() {
        if (mMediaPlayer != null) {
            mMediaPlayer.release();
        }
        if (mLibVLC != null) {
            mLibVLC.release();
        }
    }

    //https://blog.csdn.net/da_caoyuan/article/details/127409962
}
